package org.springframework.nio;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.nio.handler.SimpHandlerMapping;
import org.springframework.nio.handler.adapter.SimpHandlerAdapter;
import org.springframework.nio.method.HandlerMethod;
import org.springframework.stereotype.Component;
@Component
public class DefaultMessageListener implements ApplicationContextAware ,MessageListenner{

	protected ApplicationContext context;
	@Autowired
	private SimpHandlerMapping simpHandlerMapping;
	@Autowired
	private SimpHandlerAdapter simpHandlerAdapter;
	
	
	@Override
	public void setApplicationContext(ApplicationContext context)
			throws BeansException {
		this.context = context;
		
	}


	@Override
	public void messageRecived(NioRequest<?> request) {
		HandlerMethod handlerMethod = simpHandlerMapping.getHandler(request);
		if(handlerMethod != null){
			if(simpHandlerAdapter.support(handlerMethod)){
				try {
					simpHandlerAdapter.handle(request, handlerMethod);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	
}
